﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Grizzy_X
{
    public class CPolygonLayer : CLayer
    {
        public List<CPolygon> Polygon_Set;
        public Bitmap pnlayer;

        public bool NewPolygonLayer(int id, string name, string path, string des)
        {
            try
            {
                ID = id;
                Name = name;
                Path = path;
                Layer_Type = LayerType.CPolygonLayer;
                bSelected = true;
                bVisible = true;
                Polygon_Set = new List<CPolygon>();
                Description = des;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddPolygon(CPolygon NewPolygon)
        {
            try
            {
                Polygon_Set.Add(NewPolygon);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeletePolygon()
        {
            try
            {
                for (int i = 0; i < Controller.Selected.Count(); i++)
                {
                    Polygon_Set.RemoveAt(Controller.Selected[i]);
                    Polygon_Set.Insert(Controller.Selected[i], new CPolygon());
                    Polygon_Set[Controller.Selected[i]].Polygons = new List<List<PointF>>();
                }
                for (int i = 0; i < Polygon_Set.Count(); i++)
                {
                    if (Polygon_Set[i].Polygons.Count() == 0)
                    {
                        Polygon_Set.RemoveAt(i);
                        i--;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //实现方法有待考虑
        public bool Union()
        {
            CPolygon tempplgn = new CPolygon();
            int id = Polygon_Set[Controller.Selected[0]].GetID();
            tempplgn.newCpolygon(id, Polygon_Set[Controller.Selected[0]].LegendID);
            for (int i = 0; i < Controller.Selected.Count(); i++)
            {
                for (int j = 0; j < Polygon_Set[Controller.Selected[i]].Polygons.Count(); j++)
                {
                    tempplgn.Polygons.Add(Polygon_Set[Controller.Selected[i]].Polygons[j]);
                }
            }
            tempplgn.updateMBR();
            DeletePolygon();
            Polygon_Set.Add(tempplgn);
            Controller.Selected = new List<int>();
            Controller.Selected.Add(Polygon_Set.Count() - 1);
            return true;
        }

        public bool unUnion()
        {
            try
            {
                for (int i = 0; i < Polygon_Set[Controller.Selected[0]].Polygons.Count(); i++)
                {
                    CPolygon temp = new CPolygon();
                    //完善生成ID的步骤、重新生成外包矩形等操作，调用一些函数。。。
                    temp.newCpolygon(IDGenerator.LineID(), Polygon_Set[Controller.Selected[0]].LegendID);
                    temp.Polygons.Add(Polygon_Set[Controller.Selected[0]].Polygons[i]);
                    temp.updateMBR();
                    Polygon_Set.Add(temp);
                }
                Polygon_Set.RemoveAt(Controller.Selected[0]);
                Controller.Selected = new List<int>();
                Controller.Selected.Add(Polygon_Set.Count() - 1);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool snapsame=false;
        public void snapsameegde(PointF e)
        {
            snapsame = true;
            if (Controller.snapsameedge.Count() == 0)
            {
                snapnode(e);
                if (Controller.snapsameedge.Count() == 0)
                    snapedge(e);
            }
            else if(Controller.snapsameedge.Count()>0&&Controller.snapsameedge[1]==-1)
                snapnode(e);
            else if (Controller.snapsameedge.Count() > 0 && Controller.snapsameedge[1] == -2)
                snapedge(e);
        }
        public Point snapnode(PointF e)
        {
            Point cursorp = new Point(-1, -1);
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            float Buffer = 13 * trans;
            for (int m = 0; m < Polygon_Set.Count(); m++)
            {
                for (int i = 0; i < Polygon_Set[m].Polygons.Count(); i++)
                {
                    for (int j = 0; j < Polygon_Set[m].Polygons[i].Count(); j++)
                    {
                        if (Math.Abs(Polygon_Set[m].Polygons[i][j].X - e.X) < Buffer
                            && Math.Abs(Polygon_Set[m].Polygons[i][j].Y - e.Y) < Buffer)
                        {
                            Point temp = Transformer.ToScreen(Polygon_Set[m].Polygons[i][j]);
                            cursorp = new Point(temp.X + 195, temp.Y + 125);
                            if (m != Polygon_Set.Count() - 1 && snapsame)
                            {
                                if (Controller.snapsameedge.Count == 0)
                                    Controller.snapsameedge.Add(m);
                                if (Controller.snapsameedge.Count == 1)
                                    Controller.snapsameedge.Add(-1);
                                    if (Controller.snapsameedge.Count() == 2)
                                        Controller.snapsameedge.Add(i);
                                    Controller.snapsameedge.Add(j);
                            }
                        }
                    }
                }
            }
            return cursorp;
        }
        public Point snapedge(PointF e)
        {
            Point cursorp = new Point(-1, -1);
             float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            float Buffer = 13 * trans;
            int count = 0;
            for (int m = 0; m < Polygon_Set.Count(); m++)
            {
                if (Polygon_Set[m].MinBR.Lx - Buffer < e.X &&
                    Polygon_Set[m].MinBR.Ly - Buffer < e.Y &&
                   Polygon_Set[m].MinBR.Rx + Buffer > e.X &&
                    Polygon_Set[m].MinBR.Ry + Buffer > e.Y)
                {
                    for (int i = 0; i < Polygon_Set[m].Polygons.Count(); i++)
                    {
                        int j = 0;
                        float PI = (float)Math.PI;
                        Buffer = Math.Max(((CPolygonLegend)Controller.MapLegends[Polygon_Set[m].LegendID]).border_weight * trans / 2, 13 * trans);
                        List<PointF> RecBuffer;
                        PointF P1, P2;
                        float slope;
                        for (j = 0; j + 1 < Polygon_Set[m].Polygons[i].Count(); j++)
                        {
                            RecBuffer = new List<PointF>();
                            P1 = Polygon_Set[m].Polygons[i][j];
                            P2 = Polygon_Set[m].Polygons[i][j + 1];
                            slope = (float)Math.Atan((P1.Y - P2.Y) / (P1.X - P2.X));
                            RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                            RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope + 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope + 0.75 * PI)));
                            RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope + 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope + 0.25 * PI)));
                            RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope - 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope - 0.25 * PI)));
                            //RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                            if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                            {
                                Point temp = Transformer.ToScreen(new PointF((P1.X + P2.X) / 2, (P1.Y + P2.Y) / 2));
                                cursorp = new Point(temp.X + 195, temp.Y + 125);
                                if (m != Polygon_Set.Count() - 1 && snapsame)
                                {
                                    if (Controller.snapsameedge.Count == 0)
                                        Controller.snapsameedge.Add(m);
                                    if (Controller.snapsameedge.Count == 1)
                                        Controller.snapsameedge.Add(-2);
                                    if (Controller.snapsameedge.Count() == 2)
                                        Controller.snapsameedge.Add(i);
                                    /*if (count != 0)
                                    {
                                        Controller.snapsameedge.RemoveAt(Controller.snapsameedge.Count() - 1);
                                        Controller.snapsameedge.RemoveAt(Controller.snapsameedge.Count() - 1);
                                    }*/
                                    Controller.snapsameedge.Add(j);
                                    Controller.snapsameedge.Add(j + 1);
                                    //count++;
                                }
                            }
                        }
                        RecBuffer = new List<PointF>();
                        P1 = Polygon_Set[m].Polygons[i][j];
                        P2 = Polygon_Set[m].Polygons[i][0];
                        slope = (float)Math.Atan((P1.Y - P2.Y) / (P1.X - P2.X));
                        RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                        RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope + 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope + 0.75 * PI)));
                        RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope + 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope + 0.25 * PI)));
                        RecBuffer.Add(new PointF(P2.X + Buffer * (float)Math.Cos(slope - 0.25 * PI), P2.Y + Buffer * (float)Math.Sin(slope - 0.25 * PI)));
                        //RecBuffer.Add(new PointF(P1.X + Buffer * (float)Math.Cos(slope - 0.75 * PI), P1.Y + Buffer * (float)Math.Sin(slope - 0.75 * PI)));
                        if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                        {
                            Point temp = Transformer.ToScreen(new PointF((P1.X + P2.X) / 2, (P1.Y + P2.Y) / 2));
                            cursorp = new Point(temp.X + 195, temp.Y + 125);
                            if (m != Polygon_Set.Count() - 1 && snapsame)
                            {
                                if (Controller.snapsameedge.Count == 0)
                                    Controller.snapsameedge.Add(m);
                                if (Controller.snapsameedge.Count == 1)
                                    Controller.snapsameedge.Add(-2);
                                if (Controller.snapsameedge.Count() == 2)
                                    Controller.snapsameedge.Add(i);
                                Controller.snapsameedge.Add(j);
                                Controller.snapsameedge.Add(0);
                            }
                            return cursorp;
                        }
                    }
                }
            }
            return cursorp;
        }
        public bool Split()
        {
            try
            {
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool drawisland=false;
        //没写完
        public Bitmap drawpolygon()
        {
            /*if (drawisland)
            {
                if (Controller.Selected.Count == 1 )
                {
                    int index = Controller.Selected[0];
                    Controller.Selected = new List<int>();
                }
            }*/
            pnlayer = drawnoselect();
            drawselect();
            pnlayer = drawselectbounds(pnlayer);
            //drawselect();
            if (Controller.selectsimple != null)
            {
                if (Controller.selectsimple.Count > 0)
                    pnlayer = Polygon_Set[Controller.Selected[0]].drawsimplepolygon(pnlayer);
            }
            return pnlayer;
        }
        public Bitmap drawnoselect()
        {
            Bitmap bmp_new = new Bitmap(Controller.View_Width, Controller.View_Height);
            Graphics a = Graphics.FromImage(bmp_new);
            CPolygonLegend cpl = new CPolygonLegend();
            bool sign = true;
            Point screenp1 = new Point();
            Point screenp2 = new Point();
            for (int i = 0; i < Polygon_Set.Count(); i++)
            {
                for (int m = 0; m < Controller.Selected.Count(); m++)
                {
                    if (i == Controller.Selected[m])
                        sign = false;
                }
                if (sign)
                {
                    cpl = (CPolygonLegend)Controller.MapLegends[Polygon_Set[i].LegendID];
                    Polygon_Set[i].polygonpen = new Pen(cpl.border_color, cpl.border_weight);
                    if (cpl.Border_Style == CPolygonLegend.BorderStyle.solidline)
                        Polygon_Set[i].polygonpen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                    else if (cpl.Border_Style == CPolygonLegend.BorderStyle.dashline)
                    {
                        float[] dashValues = { 3, 3 };
                        Polygon_Set[i].polygonpen.DashPattern = dashValues;
                    }
                    if (cpl.Fill_Style == CPolygonLegend.FillStyle.solid)
                        Polygon_Set[i].polygonbrush = new SolidBrush(cpl.fill_color);
                    else if (cpl.Fill_Style == CPolygonLegend.FillStyle.transparent)
                        Polygon_Set[i].polygonbrush = new SolidBrush(Color.Transparent);
                    for (int j = 0; j < Polygon_Set[i].Polygons.Count(); j++)
                    {
                        Point[] polygonpoints = new Point[Polygon_Set[i].Polygons[j].Count()];
                        int m;
                        if (j == 0 && Polygon_Set[i].island != null)
                        {
                            a=Polygon_Set[i].enableisland(a);
                            continue;
                        }
                        for (m = 0; m + 1 < Polygon_Set[i].Polygons[j].Count(); m++)
                        {
                            screenp1 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m]);
                            screenp2 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m + 1]);
                            a.DrawLine(Polygon_Set[i].polygonpen, screenp1, screenp2);
                            a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                            polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                        }
                        screenp1 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m]);
                        a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                        polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                        screenp2 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][0]);
                        a.DrawLine(Polygon_Set[i].polygonpen, screenp1, screenp2);
                        a.FillPolygon(Polygon_Set[i].polygonbrush, polygonpoints);
                    }
                }
                sign = true;
            }
            return bmp_new;
        }
        public void drawselect()
        {           
            Graphics a = Graphics.FromImage(pnlayer);
            CPolygonLegend cpl = new CPolygonLegend();
            Point screenp1 = new Point();
            Point screenp2 = new Point();
            for (int i = 0; i < Controller.Selected.Count(); i++)
            {
                cpl = (CPolygonLegend)Controller.MapLegends[Polygon_Set[Controller.Selected[i]].LegendID];
                Polygon_Set[Controller.Selected[i]].polygonpen = new Pen(cpl.border_color, cpl.border_weight);
                if (cpl.Border_Style == CPolygonLegend.BorderStyle.solidline)
                    Polygon_Set[Controller.Selected[i]].polygonpen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                else if (cpl.Border_Style == CPolygonLegend.BorderStyle.dashline)
                {
                    float[] dashValues = { 3, 3 };
                    Polygon_Set[Controller.Selected[i]].polygonpen.DashPattern = dashValues;
                }
                if (cpl.Fill_Style == CPolygonLegend.FillStyle.solid)
                    Polygon_Set[Controller.Selected[i]].polygonbrush = new SolidBrush(cpl.fill_color);
                else if (cpl.Fill_Style == CPolygonLegend.FillStyle.transparent)
                    Polygon_Set[Controller.Selected[i]].polygonbrush = new SolidBrush(Color.Transparent);
                for (int j = 0; j < Polygon_Set[Controller.Selected[i]].Polygons.Count(); j++)
                {
                    if (j == 0 && Polygon_Set[Controller.Selected[i]].island != null)
                    {
                        a = Polygon_Set[Controller.Selected[i]].enableisland(a);
                        continue;
                    }
                        Point[] polygonpoints = new Point[Polygon_Set[Controller.Selected[i]].Polygons[j].Count()];
                        int m;
                        for (m = 0; m + 1 < Polygon_Set[Controller.Selected[i]].Polygons[j].Count(); m++)
                        {
                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][m]);
                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][m + 1]);
                            //a.DrawLine(Polygon_Set[Controller.Selected[i]].polygonpen, screenp1, screenp2);
                            //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                            polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                        }
                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][m]);
                        //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                        polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][0]);
                        //a.DrawLine(Polygon_Set[Controller.Selected[i]].polygonpen, screenp1, screenp2);
                        a.FillPolygon(Polygon_Set[Controller.Selected[i]].polygonbrush, polygonpoints);
                }
            }          
        }
        public Bitmap drawselectbounds(Bitmap currentbmp)
        {
            Graphics a = Graphics.FromImage(currentbmp);
            Point screenp1 = new Point();
            Point screenp2 = new Point();
            if (Controller.selectsimple != null)
            {
                if (Controller.selectsimple.Count > 0)
                {
                    CPolygonLegend cpl = new CPolygonLegend();
                    cpl = (CPolygonLegend)Controller.MapLegends[Polygon_Set[Controller.Selected[0]].LegendID];
                    Polygon_Set[Controller.Selected[0]].polygonpen = new Pen(cpl.border_color, cpl.border_weight);
                    if (cpl.Border_Style == CPolygonLegend.BorderStyle.solidline)
                        Polygon_Set[Controller.Selected[0]].polygonpen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                    else if (cpl.Border_Style == CPolygonLegend.BorderStyle.dashline)
                    {
                        float[] dashValues = { 3, 3 };
                        Polygon_Set[Controller.Selected[0]].polygonpen.DashPattern = dashValues;
                    }
                    if (cpl.Fill_Style == CPolygonLegend.FillStyle.solid)
                        Polygon_Set[Controller.Selected[0]].polygonbrush = new SolidBrush(cpl.fill_color);
                    else if (cpl.Fill_Style == CPolygonLegend.FillStyle.transparent)
                        Polygon_Set[Controller.Selected[0]].polygonbrush = new SolidBrush(Color.Transparent);
                    bool sign = true;
                    for (int j = 0; j < Polygon_Set[Controller.Selected[0]].Polygons.Count(); j++)
                    {
                        for (int n = 0; n < Controller.selectsimple.Count(); n++)
                        {
                            if (j == Controller.selectsimple[n])
                                sign = false;
                        }
                        if (sign)
                        {
                            if (j == 0 && Polygon_Set[Controller.Selected[0]].island != null)
                            {
                                a = Polygon_Set[Controller.Selected[0]].enableisland(a);
                                continue ;
                            }
                            Point[] polygonpoints = new Point[Polygon_Set[Controller.Selected[0]].Polygons[j].Count()];
                            int m;
                            for (m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].Polygons[j].Count(); m++)
                            {
                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m + 1]);
                                //a.DrawLine(Polygon_Set[Controller.Selected[i]].polygonpen, screenp1, screenp2);
                                //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                                polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                            }
                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                            //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                            polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][0]);
                            //a.DrawLine(Polygon_Set[Controller.Selected[i]].polygonpen, screenp1, screenp2);
                            a.FillPolygon(Polygon_Set[Controller.Selected[0]].polygonbrush, polygonpoints);
                        }
                        sign = true;
                    }
                }
            }
            for (int i = 0; i < Controller.Selected.Count(); i++)
            {
                Pen polygonpen = new Pen(Color.Transparent);                
                if (Polygon_Set[Controller.Selected[i]].polygonpen.Color == Color.SkyBlue)
                    polygonpen.Color = Color.Blue;
                else
                    polygonpen.Color = Color.SkyBlue;
                //CPolygonLegend cpl = new CPolygonLegend();

                if (Polygon_Set[Controller.Selected[i]].polygonpen.DashStyle!=System.Drawing.Drawing2D.DashStyle.Solid)
                {
                    float[] dashValues = { 3, 3 };
                    polygonpen.DashPattern = dashValues;
                }
                polygonpen.Width = Polygon_Set[Controller.Selected[i]].polygonpen.Width +2;
                for (int j = 0; j < Polygon_Set[Controller.Selected[i]].Polygons.Count(); j++)
                {
                    int m;
                    if (j == 0 && Polygon_Set[Controller.Selected[i]].island != null)
                    {
                        for (m = 0; m + 1 < Polygon_Set[Controller.Selected[i]].island.Count(); m++)
                        {
                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].island[m]);
                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].island[m+1]);
                            a.DrawLine(polygonpen, screenp1, screenp2);
                        }
                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].island[m]);
                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].island[0]);
                        a.DrawLine(polygonpen, screenp1, screenp2);
                    }
                    for (m = 0; m + 1 < Polygon_Set[Controller.Selected[i]].Polygons[j].Count(); m++)
                    {
                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][m]);
                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][m + 1]);
                        a.DrawLine(polygonpen, screenp1, screenp2);
                        //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                    }
                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][m]);
                    //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[i]].Polygons[j][0]);
                    a.DrawLine(polygonpen, screenp1, screenp2);                    
                }
                if (Controller.Selected.Count() == 1)
                {
                    if (Controller.selectsimple!= null)
                    {
                        if (Controller.selectsimple.Count() > 0)
                        {
                            polygonpen.Color = Color.Red;
                            for (int n = 0; n < Controller.selectsimple.Count(); n++)
                            {
                                int m;
                                if (Controller.selectsimple[n] == 0 && Polygon_Set[Controller.Selected[0]].island != null)
                                {
                                    for (m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].island.Count(); m++)
                                    {
                                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[m]);
                                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[m + 1]);
                                        a.DrawLine(polygonpen, screenp1, screenp2);
                                    }
                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[m]);
                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[0]);
                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                }
                                for (m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectsimple[n]].Count(); m++)
                                {
                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectsimple[n]][m]);
                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectsimple[n]][m + 1]);
                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                }
                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectsimple[n]][m]);
                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectsimple[n]][0]);
                                a.DrawLine(polygonpen, screenp1, screenp2);
                            }
                        }
                        if (Controller.current_Operation == Controller.OperationStyles.Editnode)
                        {
                            for (int j = 0; j < Polygon_Set[Controller.Selected[0]].Polygons.Count(); j++)
                            {
                                int m;
                                for (m = 0; m < Polygon_Set[Controller.Selected[0]].Polygons[j].Count(); m++)
                                {
                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                                    a.FillRectangle(Brushes.Tomato, screenp1.X - 3, screenp1.Y - 3, 6, 6);
                                }
                            }
                            if (Controller.selectnodeindex.X != -1)
                            {
                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectnodeindex.X][Controller.selectnodeindex.Y]);
                                a.FillRectangle(Brushes.Red, screenp1.X - 4, screenp1.Y - 4, 8, 8);
                            }
                            if (Controller.selectlineindex.X != -1)
                            {
                                polygonpen.Color = Color.Red;
                                if (Controller.selectlineindex.Y == Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectlineindex.X].Count() - 1)
                                {
                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectlineindex.X][Controller.selectlineindex.Y]);
                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectlineindex.X][0]);
                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                }
                                else
                                {
                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectlineindex.X][Controller.selectlineindex.Y]);
                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[Controller.selectlineindex.X][Controller.selectlineindex.Y + 1]);
                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                }
                            }
                        }
                    }
                }
            }          
            return currentbmp;
        }

        private int countsign;
        public int sign,sign2;
        public Bitmap drawnode(bool doubleclick, int index)
        {
            if (Polygon_Set[index].Polygons[0].Count() == 1 || doubleclick)
            {
                drawpolygon();
            }
            Graphics a = Graphics.FromImage(pnlayer);
            Point screenp1 = new Point();
            screenp1 = Transformer.ToScreen(Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1][Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Count() - 1]);
            if(Controller.Selected.Count()!=1)
                a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
            Point screenp2=new Point();
            if (Controller.snapsameedge != null)
            {
                if (Controller.snapsameedge.Count() > 3)
                {
                    
                    Pen polygonpen = new Pen(Color.Transparent);
                    if (Polygon_Set[index].polygonpen.Color == Color.SkyBlue)
                        polygonpen.Color = Color.Blue;
                    else
                        polygonpen.Color = Color.SkyBlue;
                    if (Polygon_Set[index].polygonpen.DashStyle != System.Drawing.Drawing2D.DashStyle.Solid)
                    {
                        float[] dashValues = { 3, 3 };
                        polygonpen.DashPattern = dashValues;
                    }
                    polygonpen.Width = Polygon_Set[index].polygonpen.Width + 2;
                    
                    if (Controller.snapsameedge[1] == -1)
                    {
                        if (!doubleclick)
                        {
                            int count = Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count();
                            if (Controller.snapsameedge.Count == 5)
                            {
                                if (sign != -1)
                                {
                                    int max = Math.Max(Controller.snapsameedge[3], Controller.snapsameedge[4]);
                                    int min = Math.Min(Controller.snapsameedge[3], Controller.snapsameedge[4]);
                                    int last = Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Count() - 1;
                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].RemoveAt(last);
                                    last = Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Count() - 1;
                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].RemoveAt(last);
                                    if (2 * (max - min) <= count)
                                    {
                                        countsign = max - min + 1;
                                        for (int i = min; i < max; i++)
                                        {
                                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                            a.DrawLine(polygonpen, screenp1, screenp2);
                                        }
                                        if (Controller.snapsameedge[3] < Controller.snapsameedge[4])
                                        {
                                            for (int i = Controller.snapsameedge[3]; i <= Controller.snapsameedge[4]; i++)
                                            {
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                        }
                                        else
                                        {
                                            for (int i = Controller.snapsameedge[3]; i >= Controller.snapsameedge[4]; i--)
                                            {
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        countsign = count - (max - min) + 1;
                                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][count - 1]);
                                        a.DrawLine(polygonpen, screenp1, screenp2);
                                        for (int i = 0; i < min; i++)
                                        {
                                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                            a.DrawLine(polygonpen, screenp1, screenp2);
                                        }
                                        for (int i = max; i < count - 1; i++)
                                        {
                                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                            a.DrawLine(polygonpen, screenp1, screenp2);
                                        }
                                        if (Controller.snapsameedge[3] < Controller.snapsameedge[4])
                                        {
                                            for (int i = Controller.snapsameedge[3]; i >= 0; i--)
                                            {
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            for (int i = count - 1; i >= Controller.snapsameedge[4]; i--)
                                            {
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                        }
                                        else
                                        {
                                            for (int i = Controller.snapsameedge[3]; i <= count - 1; i++)
                                            {
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            for (int i = 0; i <= Controller.snapsameedge[4]; i++)
                                            {
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                        }
                                    }
                                    sign = -1;
                                }
                            }
                            if (Controller.snapsameedge.Count == 6)
                            {
                                if(countsign>0)
                                {
                                for (int i = 0; i <= countsign; i++)
                                {
                                    int last = Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Count() - 1;
                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].RemoveAt(last);
                                }
                                //int count = Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count();
                                int max = Math.Max(Controller.snapsameedge[3], Controller.snapsameedge[5]);
                                int min = Math.Min(Controller.snapsameedge[3], Controller.snapsameedge[5]);
                                if (Controller.snapsameedge[4] < max && Controller.snapsameedge[4] > min)
                                {
                                    for (int i = min; i < max; i++)
                                    {
                                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                        a.DrawLine(polygonpen, screenp1, screenp2);
                                    }
                                    if (Controller.snapsameedge[3] < Controller.snapsameedge[5])
                                    {
                                        for (int i = Controller.snapsameedge[3]; i <= Controller.snapsameedge[5]; i++)
                                        {
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                    }
                                    else
                                    {
                                        for (int i = Controller.snapsameedge[3]; i >= Controller.snapsameedge[5]; i--)
                                        {
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                    }
                                }
                                else
                                {
                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][count - 1]);
                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                    for (int i = 0; i < min; i++)
                                    {
                                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                        a.DrawLine(polygonpen, screenp1, screenp2);
                                    }
                                    for (int i = max; i < count - 1; i++)
                                    {
                                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                        a.DrawLine(polygonpen, screenp1, screenp2);
                                    }
                                    if (Controller.snapsameedge[3] < Controller.snapsameedge[5])
                                    {
                                        for (int i = Controller.snapsameedge[3]; i >= 0; i--)
                                        {
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                        for (int i = count - 1; i >= Controller.snapsameedge[5]; i--)
                                        {
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                    }
                                    else
                                    {
                                        for (int i = Controller.snapsameedge[3]; i <= count - 1; i++)
                                        {
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                        for (int i = 0; i <= Controller.snapsameedge[5]; i++)
                                        {
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                    }
                                }
                                countsign = 0;
                            }
                            }
                        }
                    }
                    if (Controller.snapsameedge[1] == -2)
                    {
                        if (!doubleclick)
                        {
                            int count = Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count();
                            if (Controller.snapsameedge.Count == 5)
                            {
                                if (sign != -1)
                                {
                                    int last = Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Count() - 1;
                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].RemoveAt(last);
                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[3]]);
                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[4]]);
                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[3]]);
                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[4]]);
                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                    sign = -1;
                                }
                            }
                            if (Controller.snapsameedge.Count == 7)
                            {
                                if (sign2 != -1)
                                {
                                    int start;
                                    int end;
                                    for (int i = 1; i < 3; i++)
                                    {
                                        int last = Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Count() - 1;
                                        Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].RemoveAt(last);
                                    }
                                    int max = Math.Max(Controller.snapsameedge[3], Controller.snapsameedge[6]);
                                    int min = Math.Min(Controller.snapsameedge[3], Controller.snapsameedge[6]);
                                    if (Controller.snapsameedge[4] != 0 && Controller.snapsameedge[6] != 0)
                                    {
                                        if (2 * (max - min) < count)
                                        {
                                            countsign = max - min + 1;
                                            start = Controller.snapsameedge[3];
                                            end = Controller.snapsameedge[6];
                                            for (int i = min; i < max; i++)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                            }
                                            if (Controller.snapsameedge[3] < Controller.snapsameedge[6])
                                            {
                                                for (int i = Controller.snapsameedge[3]; i <= Controller.snapsameedge[6]; i++)
                                                {
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                            }
                                            else
                                            {
                                                for (int i = Controller.snapsameedge[3]; i >= Controller.snapsameedge[6]; i--)
                                                {
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                            a.DrawLine(polygonpen, screenp1, screenp2);
                                            for (int i = max - 1; i < Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1; i++)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                            }
                                            for (int i = 0; i < min + 1; i++)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                            }
                                            if (Controller.snapsameedge[4] <= Controller.snapsameedge[5])
                                            {
                                                for (int i = Controller.snapsameedge[4]; i >= 0; i--)
                                                {
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                                for (int i = Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1; i >= Controller.snapsameedge[5]; i--)
                                                {
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                            }
                                            else
                                            {
                                                for (int i = Controller.snapsameedge[4]; i <= Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1; i++)
                                                {
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                                for (int i = 0; i <= Controller.snapsameedge[5]; i++)
                                                {
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                            }
                                        }
                                    }
                                    if (Controller.snapsameedge[4] == 0 || Controller.snapsameedge[6] == 0)
                                    {
                                        if (Controller.snapsameedge[4] == 0)
                                        {
                                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                            a.DrawLine(polygonpen, screenp1, screenp2);
                                            if (2 * (1 + Controller.snapsameedge[6]) <= count)
                                            {
                                                countsign = 1 + Controller.snapsameedge[6];
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                                int i;
                                                for (i = 0; i < Controller.snapsameedge[6]; i++)
                                                {
                                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            else
                                            {
                                                countsign = count - (1 + Controller.snapsameedge[6]);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                                int i = Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1;
                                                for (; i > Controller.snapsameedge[5]; i--)
                                                {
                                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i - 1]);
                                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                        }
                                        if (Controller.snapsameedge[6] == 0)
                                        {
                                            if (2 * (1 + Controller.snapsameedge[4]) <= count)
                                            {
                                                countsign = 1 + Controller.snapsameedge[6];
                                                /*screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);*/
                                                int i;
                                                for (i = Controller.snapsameedge[6]; i > 0; i--)
                                                {
                                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i - 1]);
                                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                            }
                                            else
                                            {
                                                countsign = count - (1 + Controller.snapsameedge[4]);
                                                int i = Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1;
                                                for (i = Controller.snapsameedge[3]; i < Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1; i++)
                                                {
                                                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                    a.DrawLine(polygonpen, screenp1, screenp2);
                                                    Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                }
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                            }
                                        }
                                    }
                                }
                                sign2 = -1;
                            }
                            if (Controller.snapsameedge.Count == 9)
                            {
                                if (countsign > 0)
                                {
                                    for (int i = 0; i <= countsign; i++)
                                    {
                                        int last = Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Count() - 1;
                                        Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].RemoveAt(last);
                                    }
                                    //if(Controller.snapsameedge[3]!=0&&Controller.snapsameedge[8]!=0)
                                    // {
                                    int min, max;
                                    min = Math.Min(Controller.snapsameedge[3], Controller.snapsameedge[7]);
                                    max = Math.Max(Controller.snapsameedge[3], Controller.snapsameedge[7]);
                                    if (Controller.snapsameedge[5] > min && Controller.snapsameedge[5] < max)
                                    {
                                        if (Controller.snapsameedge[3] < Controller.snapsameedge[7])
                                        {
                                            int i;
                                            for (i = Controller.snapsameedge[3]; i < Controller.snapsameedge[7]; i++)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[7]]);
                                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[8]]);
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[8]]);
                                            a.DrawLine(polygonpen, screenp1, screenp2);
                                        }
                                        else
                                        {
                                            int i;
                                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[4]]);
                                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[3]]);
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[4]]);
                                            a.DrawLine(polygonpen, screenp1, screenp2);
                                            for (i = Controller.snapsameedge[3]; i > Controller.snapsameedge[7]; i--)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i - 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                    }
                                    else
                                    {
                                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1]);
                                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][0]);
                                        a.DrawLine(polygonpen, screenp1, screenp2);
                                        if (Controller.snapsameedge[3] < Controller.snapsameedge[7])
                                        {
                                            int i;
                                            for (i = Controller.snapsameedge[4]; i > 0; i--)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i - 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            i = Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1;
                                            for (; i > Controller.snapsameedge[7]; i--)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i - 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                        else
                                        {
                                            int i;
                                            for (i = Controller.snapsameedge[3]; i < Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1; i++)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            //i=Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]].Count() - 1;
                                            for (i = 0; i < Controller.snapsameedge[8]; i++)
                                            {
                                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i + 1]);
                                                a.DrawLine(polygonpen, screenp1, screenp2);
                                                Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                            }
                                            Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count() - 1].Add(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][i]);
                                        }
                                    }
                                    countsign = 0;
                                }
                                }
                            }
                        }
                    }
                    if (Controller.snapsameedge.Count > 3)
                    {
                        for (int i = 3; i < Controller.snapsameedge.Count(); i++)
                        {
                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.snapsameedge[0]].Polygons[Controller.snapsameedge[2]][Controller.snapsameedge[i]]);
                            a.FillRectangle(Brushes.Red, screenp1.X - 4, screenp1.Y - 4, 8, 8);
                        }
                    }
                }
            return pnlayer;
        }

        public Bitmap drawline(Point startPoint, Point endPoint,int index)
        {
            pnlayer = new Bitmap(Controller.View_Width, Controller.View_Height);
            Graphics a = Graphics.FromImage(pnlayer);
            /*if (Controller.Selected.Count() == 1)
            {
                Point screenp1 = new Point();
                Point screenp2 = new Point();
                Pen polygonpen = new Pen(Color.Transparent);
                if (Polygon_Set[Controller.Selected[0]].polygonpen.Color == Color.SkyBlue)
                    polygonpen.Color = Color.Blue;
                else
                    polygonpen.Color = Color.SkyBlue;
                //CPolygonLegend cpl = new CPolygonLegend();

                if (Polygon_Set[Controller.Selected[0]].polygonpen.DashStyle != System.Drawing.Drawing2D.DashStyle.Solid)
                {
                    float[] dashValues = { 3, 3 };
                    polygonpen.DashPattern = dashValues;
                }
                polygonpen.Width = Polygon_Set[Controller.Selected[0]].polygonpen.Width + 2;
                for (int j = 0; j < Polygon_Set[Controller.Selected[0]].Polygons.Count() - 1; j++)
                {
                    int m;
                    Point[] polygonpoints = new Point[Polygon_Set[Controller.Selected[0]].Polygons[j].Count()];
                    for (m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].Polygons[j].Count(); m++)
                    {
<<<<<<< .mine
                        screenp1 = Viewer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
=======
                        Point screenp1 = new Point();
                        Point screenp2 = new Point();
                        Point[] polygonpoints = new Point[Polygon_Set[i].Polygons[j].Count()];
                        int m;
                        for (m = 0; m + 1 < Polygon_Set[i].Polygons[j].Count(); m++)
                        {
                            screenp1 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m]);
                            screenp2 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m + 1]);
                            a.DrawLine(Polygon_Set[i].polygonpen, screenp1, screenp2);
                            a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                            polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                        }
                        screenp1 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m]);
                        a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
>>>>>>> .r124
                        polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    }
                    screenp1 = Viewer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                    polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    a.FillPolygon(Polygon_Set[Controller.Selected[0]].polygonbrush, polygonpoints);
                }
                for (int j = 0; j < Polygon_Set[Controller.Selected[0]].Polygons.Count() - 1; j++)
                {
                    int m;
                    //Point[] polygonpoints = new Point[Polygon_Set[Controller.Selected[0]].Polygons[j].Count()];
                    for (m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].Polygons[j].Count(); m++)
                    {
                        screenp1 = Viewer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                        screenp2 = Viewer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m + 1]);
                        a.DrawLine(polygonpen, screenp1, screenp2);
                        //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                        //polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    }
                    screenp1 = Viewer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                    //polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                    screenp2 = Viewer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][0]);
                    a.DrawLine(polygonpen, screenp1, screenp2);
                    //a.FillPolygon(Polygon_Set[Controller.Selected[0]].polygonbrush, polygonpoints);
                }
            }*/
            for (int i = 0; i < Polygon_Set.Count(); i++)                              //重绘已存储的边
            {
                //if (Controller.Selected.Count != 1 || i != Controller.Selected[0])
                //{
                    if (Polygon_Set[i].Polygons != null)
                    {
                        for (int j = 0; j < Polygon_Set[i].Polygons.Count(); j++)
                        {
                            if (Controller.Selected.Count != 1 || i != Controller.Selected[0])
                            {
                                Point screenp1 = new Point();
                                Point screenp2 = new Point();
                                if (j == 0 && Polygon_Set[i].island != null)
                                {
                                    a = Polygon_Set[i].enableisland(a);
                                    continue;
                                }
                                Point[] polygonpoints = new Point[Polygon_Set[i].Polygons[j].Count()];
                                int m;
                                for (m = 0; m + 1 < Polygon_Set[i].Polygons[j].Count(); m++)
                                {
                                    screenp1 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m]);
                                    screenp2 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m + 1]);
                                    a.DrawLine(Polygon_Set[i].polygonpen, screenp1, screenp2);
                                    a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                                    polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                                }
                                screenp1 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][m]);
                                a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                                polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                                if (i !=index || j + 1 < Polygon_Set[i].Polygons.Count())
                                {
                                    screenp2 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][0]);
                                    a.DrawLine(Polygon_Set[i].polygonpen, screenp1, screenp2);
                                    a.FillPolygon(Polygon_Set[i].polygonbrush, polygonpoints);
                                }
                            }
                            /*screenp2 = Transformer.ToScreen(Polygon_Set[i].Polygons[j][0]);
                            a.DrawLine(Polygon_Set[i].polygonpen, screenp1, screenp2);
                            a.FillPolygon(Polygon_Set[i].polygonbrush, polygonpoints);*/
                        }
                    }
                //}
            }
            if (Controller.Selected.Count() == 1)
            {
                Point screenp1 = new Point();
                Point screenp2 = new Point();               
                Pen polygonpen = new Pen(Color.Transparent);              
                if (Polygon_Set[Controller.Selected[0]].polygonpen.Color == Color.SkyBlue)
                    polygonpen.Color = Color.Blue;
                else
                    polygonpen.Color = Color.SkyBlue;
                //CPolygonLegend cpl = new CPolygonLegend();

                if (Polygon_Set[Controller.Selected[0]].polygonpen.DashStyle!=System.Drawing.Drawing2D.DashStyle.Solid)
                {
                    float[] dashValues = { 3, 3 };
                    polygonpen.DashPattern = dashValues;
                }
                polygonpen.Width = Polygon_Set[Controller.Selected[0]].polygonpen.Width + 2;
                int j;
                for (j = 0; j < Polygon_Set[Controller.Selected[0]].Polygons.Count() - 1; j++)
                {
                    int m;
                    if (!drawisland)
                    {
                        if (j == 0 && Polygon_Set[Controller.Selected[0]].island != null)
                        {
                            a = Polygon_Set[Controller.Selected[0]].enableisland(a);
                            continue;
                        }
                    }
                    Point[] polygonpoints = new Point[Polygon_Set[Controller.Selected[0]].Polygons[j].Count()];
                    for (m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].Polygons[j].Count(); m++)
                    {
                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                        polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    }
                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                    polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    a.FillPolygon(Polygon_Set[Controller.Selected[0]].polygonbrush, polygonpoints);
                }
                
                for (j = 0; j < Polygon_Set[Controller.Selected[0]].Polygons.Count()-1; j++)
                {
                    int m;
                    if (!drawisland)
                    {
                        if (j == 0 && Polygon_Set[Controller.Selected[0]].island != null)
                        {
                            for (m = 0; m +1< Polygon_Set[Controller.Selected[0]].island.Count(); m++)
                            {
                                screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[m]);
                                screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[m + 1]);
                                a.DrawLine(polygonpen, screenp1, screenp2);
                            }
                            screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[m]);
                            screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].island[0]);
                            a.DrawLine(polygonpen, screenp1, screenp2);
                        }
                    }
                    //Point[] polygonpoints = new Point[Polygon_Set[Controller.Selected[0]].Polygons[j].Count()];
                    for (m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].Polygons[j].Count(); m++)
                    {
                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m + 1]);
                        a.DrawLine(polygonpen, screenp1, screenp2);
                        //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                        //polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    }
                    screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                    //polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                    screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][0]);
                    a.DrawLine(polygonpen, screenp1, screenp2);
                }
                    //j++;
                    for (int m = 0; m + 1 < Polygon_Set[Controller.Selected[0]].Polygons[j].Count(); m++)
                    {
                        screenp1 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m]);
                        screenp2 = Transformer.ToScreen(Polygon_Set[Controller.Selected[0]].Polygons[j][m + 1]);
                        a.DrawLine(Polygon_Set[Controller.Selected[0]].polygonpen, screenp1, screenp2);
                        //a.FillRectangle(Brushes.Blue, screenp1.X - 2, screenp1.Y - 2, 4, 4);
                        //polygonpoints[m] = new Point(screenp1.X, screenp1.Y);
                    }            
                    //a.FillPolygon(Polygon_Set[Controller.Selected[0]].polygonbrush, polygonpoints);
                }           
            //if(Controller.Selected.Count()!=1)
                a.DrawLine(Polygon_Set[index].polygonpen, startPoint, endPoint);
            Point screen=Transformer.ToScreen(Polygon_Set[index].Polygons[Polygon_Set[index].Polygons.Count()-1][0]);
                a.DrawLine(Polygon_Set[index].polygonpen, screen, endPoint);
            return pnlayer;
        }
        public void SelectPolygon(PointF e)
        {
            for (int i = 0; i < Polygon_Set.Count(); i++)
            {
                float trans;
                if (Controller.MyMap.Unit == "英寸")
                    trans = (float)1 / 96;
                else
                    trans = (float)2.54 / 96;
                float Buffer = Math.Max(((CPolygonLegend)Controller.MapLegends[Polygon_Set[i].LegendID]).border_weight * trans / 2, 10 * trans);
                if (Polygon_Set[i].MinBR.Lx - Buffer < e.X && Polygon_Set[i].MinBR.Ly - Buffer < e.Y &&
                    Polygon_Set[i].MinBR.Rx + Buffer > e.X && Polygon_Set[i].MinBR.Ry + Buffer > e.Y)
                {
                    for (int j = 0; j < Polygon_Set[i].Polygons.Count(); j++)
                    {
                        List<PointF> RecBuffer = new List<PointF>();
                        if (j == 0 && Polygon_Set[i].island != null)
                        {
                            for (int k = 0; k < Polygon_Set[i].island.Count(); k++)
                            {
                                RecBuffer.Add(Polygon_Set[i].island[k]);
                            }
                            if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                                break;
                        }
                        for (int k = 0; k < Polygon_Set[i].Polygons[j].Count(); k++)
                            RecBuffer.Add(Polygon_Set[i].Polygons[j][k]);
                        //RecBuffer.Add(Polygon_Set[i].Polygons[j][0]);
                        if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                        {
                            for (int m = 0; m < Controller.Selected.Count(); m++)
                            {
                                if (i == Controller.Selected[m])
                                {
                                    Polygon_Set[i].isselected = false;
                                    Controller.Selected.RemoveAt(m);
                                    return;
                                }
                            }
                            Controller.SelectedObjectType = "Polygon";
                            Controller.Selected.Add(i);
                            Polygon_Set[i].isselected = true;
                            if (Polygon_Set[i].Polygons.Count == 1 && Controller.Selected.Count == 1)
                                MainForm.pMainWin.Split.Enabled = true;
                            return;
                        }
                    }
                }
            }
        }
    }
}
